# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def hasPathSum(self, root: TreeNode, sum: int) -> bool:
        if not root:
            return False
        self.sum = sum
        return self.search(0, root)

    def search(self, now_sum, note: TreeNode):
        mid_num = now_sum + note.val
        if mid_num == self.sum and not note.left and not note.right:
            return True
        else:
            if note.left and self.search(mid_num, note.left):
                return True
            if note.right and self.search(mid_num, note.right):
                return True
            return False
